#!/bin/bash
#======================================================================================
# Function: Install openwrt to emmc for Amlogic S9xxx STB
# Copyright (C) 2020-- https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021-- https://github.com/ophub/luci-app-amlogic
#======================================================================================

# The script supports directly setting parameters for installation, skipping interactive selection
# openwrt-install-amlogic ${AUTO_MAINLINE_UBOOT} ${SOC_ID} ${DTB_FILENAME} ${SHARED_FSTYPE}
# E.g: openwrt-install-amlogic yes 1 auto_dtb ext4
# E.g: openwrt-install-amlogic no 99 meson-gxl-s905x-p212.dtb ext4
# Tip: When custom dtb file, set ${SOC_ID} to 99, and parameter ${DTB_FILENAME} must be set
# Tip: ${SHARED_FSTYPE}: Shared partition can be ext4, xfs, btrfs, f2fs
# return 0

# You can also execute the script directly, and interactively select related functions
# E.g: openwrt-install-amlogic

# Receive one-key command related parameters
AUTO_MAINLINE_UBOOT=${1}

# For [luci-app-amlogic] input parameter: SOC & DTB
# When there is no input parameter, select manually
SPECIFY_SOC=""
SPECIFY_DTB=""
if [[ -n "$(echo ${2} | sed -n "/^[0-9]\+$/p")" ]]; then
    SPECIFY_SOC=${2}
    if [[ "${2}" -eq "99" ]]; then
        if [[ -n "${3}" ]]; then
            SPECIFY_DTB=${3}
        else
            echo "Please enter the DTB file name!"
            exit 1
        fi
    else
        SPECIFY_DTB="auto_dtb"
    fi
fi

# shared partition can be ext4, xfs, btrfs, f2fs
SHARED_FSTYPE=${4}

echo "AUTO_MAINLINE_UBOOT: ${AUTO_MAINLINE_UBOOT}"
echo "SPECIFY_SOC: ${SPECIFY_SOC}"
echo "SPECIFY_DTB: ${SPECIFY_DTB}"
echo "SHARED_FSTYPE: ${SHARED_FSTYPE}"

# Current device model
MYDEVICE_NAME=$(cat /proc/device-tree/model | tr -d '\000')
if [[ -z "${MYDEVICE_NAME}" ]]; then
    echo "The device name is empty and cannot be recognized."
    exit 1
elif [[ ! -f "/etc/flippy-openwrt-release" ]]; then
    echo "The [ /etc/flippy-openwrt-release ] file is missing."
    exit 1
else
    echo -e "Current device: ${MYDEVICE_NAME} [ amlogic ]"
    sleep 3
fi

# Find the device name of /
root_devname=$(df / | tail -n1 | awk '{print $1}' | awk -F '\/' '{print substr($3, 1, length($3)-2)}')
if lsblk -l | grep -E "^${root_devname}boot0" >/dev/null; then
    echo "you are running in emmc mode, please boot system with usb or tf card!"
    exit 1
fi

install_emmc="$(lsblk -l -o NAME | grep -oE '(mmcblk[0-9]?boot0)' | sed "s/boot0//g")"
if [[ "${install_emmc}" == "" ]]; then
    echo "No emmc can be found to install the openwrt system!"
    exit 1
fi

# EMMC DEVICE NAME
EMMC_NAME="${install_emmc}"
EMMC_DEVPATH="/dev/${EMMC_NAME}"
echo ${EMMC_DEVPATH}
EMMC_SIZE=$(lsblk -l -b -o NAME,SIZE | grep ${EMMC_NAME} | sort | uniq | head -n1 | awk '{print $2}')
echo "${EMMC_NAME} : ${EMMC_SIZE} bytes"

ROOT_NAME=$(lsblk -l -o NAME,MAJ:MIN,MOUNTPOINT | grep -e '/$' | awk '{print $1}')
echo "ROOTFS: ${ROOT_NAME}"

BOOT_NAME=$(lsblk -l -o NAME,MAJ:MIN,MOUNTPOINT | grep -e '/boot$' | awk '{print $1}')
echo "BOOT: ${BOOT_NAME}"

#Choose the type of installation box
DEFAULT_FDTFILE="meson-sm1-x96-max-plus.dtb"
U_BOOT_EXT=0

# box model database
# The field separator is :
# " " or "" or NA or NULL means this field is null
# The fields list:
# 1.  id
# 2.  model name
# 3.  SOC
# 4.  FDTFILE
# 5.  UBOOT_OVERLOAD
# 6.  MAINLINE_UBOOT
# 7.  ANDROID_UBOOT
# 8.  brief description
#

# allow use external modal database
if [ -f "/etc/model_database.txt" ]; then
    model_database=$(cat /etc/model_database.txt)
else
    model_database="
# Amlogic GXL Family
11:Phicomm N1:s905d:meson-gxl-s905d-phicomm-n1.dtb:u-boot-n1.bin:NA:/lib/u-boot/u-boot-2015-phicomm-n1.bin:4C@1512Mhz,2GB Mem,1Gb Net,Wifi
12:Phicomm N1 (DMA thresh):s905d:meson-gxl-s905d-phicomm-n1-thresh.dtb:u-boot-n1.bin:NA:/lib/u-boot/u-boot-2015-phicomm-n1.bin:Same as above, when ethmac flow control is off 
13:hg680p & b860h:s905x:meson-gxl-s905x-p212.dtb:u-boot-p212.bin:NA:NA:4C@1512Mhz,1Gb Net
14:X96-Mini & TX3-Mini:s905w:meson-gxl-s905w-tx3-mini.dtb:u-boot-s905x-s912.bin:NA:NA:4C@1512Mhz,100M Net

# Amlogic GXM Family
21:Octopus Planet:s912:meson-gxm-octopus-planet.dtb:u-boot-zyxq.bin:NA:NA:4C@1512Mhz+4C@1000Mhz,2GB Mem,1Gb Net

# Amlogic G12A Family
31:X96 Max 4GB:s905x2:meson-g12a-x96-max.dtb:u-boot-x96max.bin:/lib/u-boot/x96max-u-boot.bin.sd.bin:NA:4C@1908Mhz,4GB Mem,1Gb Net
32:X96 Max 2GB:s905x2:meson-g12a-x96-max-rmii.dtb:u-boot-x96max.bin:/lib/u-boot/x96max-u-boot.bin.sd.bin:NA:4C@1908Mhz,2GB Mem,100M Net

# Amlogic G12B Family
41:Beelink GT-King:s922x:meson-g12b-gtking.dtb:u-boot-gtking.bin:/lib/u-boot/gtking-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
42:Beelink GT-King Pro:s922x:meson-g12b-gtking-pro.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
43:Beelink GT-King Pro H:s922x:meson-g12b-gtking-pro-h.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:S922X-H,2C@1800Mhz(A53)+4C@2208Mhz(A73),4GB Mem,1Gb Net,Wifi
44:Beelink GT-King Pro Rev_A:s922x:meson-g12b-gtking-pro-rev_a.dtb:u-boot-gtkingpro.bin::NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi
45:Hardkernel ODroid N2:s922x:meson-g12b-odroid-n2.dtb:u-boot-gtkingpro.bin:/lib/u-boot/odroid-n2-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net
46:UGOOS AM6 Plus:s922x:meson-g12b-ugoos-am6.dtb:u-boot-gtkingpro.bin:/lib/u-boot/gtkingpro-u-boot.bin.sd.bin:NA:2C@1800Mhz(A53)+4C@1908Mhz(A73),4GB Mem,1Gb Net,Wifi

# Amlogic SM1 Family
51:X96 Max+:s905x3:meson-sm1-x96-max-plus.dtb:u-boot-x96maxplus.bin:/lib/u-boot/x96maxplus-u-boot.bin.sd.bin:/lib/u-boot/hk1box-bootloader.img:4C@2100Mhz,4GB Mem,1Gb Net
52:X96 Max+ (OverClock):s905x3:meson-sm1-x96-max-plus-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/x96maxplus-u-boot.bin.sd.bin:/lib/u-boot/hk1box-bootloader.img:4C@2208Mhz,4GB Mem,1Gb Net
53:HK1 Box:s905x3:meson-sm1-hk1box-vontar-x3.dtb:u-boot-x96maxplus.bin:/lib/u-boot/hk1box-u-boot.bin.sd.bin:NA:4C@2100Mhz,4GB Mem,1Gb Net,Wifi
54:HK1 Box (OveClock):s905x3:meson-sm1-hk1box-vontar-x3-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/hk1box-u-boot.bin.sd.bin:NA:4C@2208Mhz,4GB Mem,1Gb Net,Wifi
55:H96 Max X3:s905x3:meson-sm1-h96-max-x3.dtb:u-boot-x96maxplus.bin:/lib/u-boot/h96maxx3-u-boot.bin.sd.bin:NA:4C@2100Mhz,4GB Mem,1Gb Net,Wifi
56:H96 Max X3:s905x3:meson-sm1-h96-max-x3-oc.dtb:u-boot-x96maxplus.bin:/lib/u-boot/h96maxx3-u-boot.bin.sd.bin:NA:4C@2208Mhz,4GB Mem,1Gb Net,Wifi
57:Ugoos X3:s905x3:meson-sm1-ugoos-x3.dtb:u-boot-ugoos-x3.bin:NA:NA:4C@2100Mhz,2(Cube)/4(Pro,Plus)GB Mem,1Gb Net,Wifi
58:Ugoos X3(OverClock):s905x3:meson-sm1-ugoos-x3.dtb:u-boot-ugoos-x3.bin:NA:NA:4C@2208Mhz,2(Cube)/4(Pro,Plus)GB Mem,1Gb Net,Wifi

# Other
0:Other::NA:NA:NA:NA:Enter the dtb file name of your box
"
fi

function display_database() {
    echo "${model_database}" | perl -ne 'chomp;if(m/^#/){s/#+\s+//;print "$_ >>>\n";}elsif($_){@f=split/:/;printf("%4s. %-30s%-8s%-s\n",$f[0],$f[1],$f[2],$f[7])}'
}

function search_model() {
    local id=$1
    local ret_count=$(echo "${model_database}" | awk -F ':' "\$1~/^$id\$/ {print \$0}" | wc -l)
    if [ $ret_count -eq 1 ]; then
        echo "${model_database}" | awk -F ':' "\$1~/^$id\$/ {print \$0}"
    fi
}

echo "Please select s9xxx box model:"
echo "----------------------------------------------------------------------------------------------------"
display_database
echo "----------------------------------------------------------------------------------------------------"

# For [luci-app-amlogic] input parameter: SOC & DTB
# When there is no input parameter, select manually
if [[ -n "${SPECIFY_SOC}" ]]; then
    boxtype=${SPECIFY_SOC}
else
    echo -n "Please choose: "
    read boxtype
fi

if [ "$boxtype" == "99" ]; then
    FDTFILE=${SPECIFY_DTB}
    UBOOT_OVERLOAD=""
    MAINLINE_UBOOT=""
    ANDROID_UBOOT=""
    AMLOGIC_SOC=""
else
    ret=$(search_model $boxtype)
    if [ "$ret" == "" ]; then
        echo "Input error, exit!"
        exit 1
    fi
    # 3.  soc
    # 4.  FDTFILE
    # 5.  UBOOT_OVERLOAD
    # 6.  MAINLINE_UBOOT
    # 7.  ANDROID_UBOOT
    AMLOGIC_SOC=$(echo "$ret" | awk -F ':' '{print $3}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
    FDTFILE=$(echo "$ret" | awk -F ':' '{print $4}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
    UBOOT_OVERLOAD=$(echo "$ret" | awk -F ':' '{print $5}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
    MAINLINE_UBOOT=$(echo "$ret" | awk -F ':' '{print $6}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
    ANDROID_UBOOT=$(echo "$ret" | awk -F ':' '{print $7}' | perl -pe 's/NA//;s/NULL//;s/\s+//g;')
fi

if [ "$FDTFILE" == "" ]; then
    cat <<EOF
Please enter the .dtb file name of your box, do not include the path.
For example: $DEFAULT_FDTFILE
EOF
    echo "Enter the .dtb File name:"
    read CUST_FDTFILE
    FDTFILE=$CUST_FDTFILE
    UBOOT_OVERLOAD=""
    MAINLINE_UBOOT=""
    ANDROID_UBOOT=""
    AMLOGIC_SOC=""
fi

echo -e "FDT Value [ ${FDTFILE} ]"

if [ ! -f "/boot/dtb/amlogic/${FDTFILE}" ]; then
    echo "/boot/dtb/amlogic/${FDTFILE} does not exist!"
    echo "You can download the .dtb file from [ https://github.com/ophub/amlogic-s9xxx-openwrt/tree/main/amlogic-s9xxx/amlogic-dtb ]"
    echo "Copy it to [ /boot/dtb/amlogic/ ]."
    echo "Then execute this Install command."
    exit 1
fi

#Check if writing to EMMC is supported
MODULES_NOW=$(ls /lib/modules/ 2>/dev/null)
VERSION_NOW=$(echo ${MODULES_NOW} | grep -oE '^[1-9].[0-9]{1,3}' 2>/dev/null)
echo -e "This Kernel [ ${MODULES_NOW} ]"

k510_ver=${VERSION_NOW%%.*}
k510_maj=${VERSION_NOW##*.}
if [ "${k510_ver}" -eq "5" ]; then
    if [ "${k510_maj}" -ge "10" ]; then
        K510="1"
    else
        K510="0"
    fi
elif [ "${k510_ver}" -gt "5" ]; then
    K510="1"
else
    K510="0"
fi

# backup old bootloader
if [ ! -f "/root/BackupOldBootloader.img" ]; then
    echo "Backup bootloader -> [ BackupOldBootloader.img ] ... "
    dd if=/dev/$EMMC_NAME of=/root/BackupOldBootloader.img bs=1M count=4 conv=fsync
    echo "Backup bootloader complete."
    echo
fi

swapoff -a

# umount all other mount points
MOUNTS=$(lsblk -l -o MOUNTPOINT)
for mnt in $MOUNTS; do
    if [ "$mnt" == "MOUNTPOINT" ]; then
        continue
    fi

    if [ "$mnt" == "" ]; then
        continue
    fi

    if [ "$mnt" == "/" ]; then
        continue
    fi

    if [ "$mnt" == "/boot" ]; then
        continue
    fi

    if [ "$mnt" == "/opt" ]; then
        continue
    fi

    if [ "$mnt" == "[SWAP]" ]; then
        echo "swapoff -a"
        swapoff -a
        continue
    fi

    if echo $mnt | grep $EMMC_NAME; then
        echo "umount -f $mnt"
        umount -f $mnt
        if [ $? -ne 0 ]; then
            echo "$mnt Cannot be uninstalled, the installation process is aborted."
            exit 1
        fi
    fi
done

# Delete old partition if exists
p=$(lsblk -l | grep -e "${EMMC_NAME}p" | wc -l)
echo "A total of [ $p ] old partitions on EMMC will be deleted"
>/tmp/fdisk.script
while [ $p -ge 1 ]; do
    echo "d" >>/tmp/fdisk.script
    if [ $p -gt 1 ]; then
        echo "$p" >>/tmp/fdisk.script
    fi
    p=$((p - 1))
done

# you can change ROOT size(MB) >= 320
ROOT1=960
ROOT2=960
if [[ "${AMLOGIC_SOC}" == "s912" || "${AMLOGIC_SOC}" == "s905d" ]]; then
    BOOT=512
    BLANK1=68
    BLANK2=184
    BLANK3=0
    BLANK4=0
elif [[ "${AMLOGIC_SOC}" == "s905x" ]]; then
    BOOT=160
    BLANK1=700
    BLANK2=0
    BLANK3=0
    BLANK4=0
else
    BOOT=160
    BLANK1=68
    BLANK2=0
    BLANK3=162
    BLANK4=0
fi

DST_TOTAL_MB=$((EMMC_SIZE / 1024 / 1024))

start1=$((BLANK1 * 2048))
end1=$((start1 + (BOOT * 2048) - 1))

start2=$(((BLANK2 * 2048) + end1 + 1))
end2=$((start2 + (ROOT1 * 2048) - 1))

start3=$(((BLANK3 * 2048) + end2 + 1))
end3=$((start3 + (ROOT2 * 2048) - 1))

start4=$(((BLANK4 * 2048) + end3 + 1))
end4=$((DST_TOTAL_MB * 2048 - 1))

cat >>/tmp/fdisk.script <<EOF
n
p
1
$start1
$end1
n
p
2
$start2
$end2
n
p
3
$start3
$end3
n
p
$start4
$end4
t
1
c
t
2
83
t
3
83
t
4
83
w
EOF

fdisk /dev/${EMMC_NAME} </tmp/fdisk.script 2>/dev/null
if [ $? -ne 0 ]; then
    echo "The fdisk partition fails, Please try again."
    dd if=/root/BackupOldBootloader.img of=/dev/${EMMC_NAME} conf=fsync && sync
    dd if=/dev/zero of=/dev/${EMMC_NAME} bs=512 count=1 && sync
    exit 1
fi
echo "Partition complete."

# write some zero data to part begin
seek=$((start1 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync

seek=$((start2 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync

seek=$((start3 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync

seek=$((start4 / 2048))
dd if=/dev/zero of=/dev/${EMMC_NAME} bs=1M count=1 seek=$seek conv=fsync

#Mainline U-BOOT detection
FLASH_MAINLINE_UBOOT=0
if [[ -n "${MAINLINE_UBOOT}" && -f "${MAINLINE_UBOOT}" ]]; then
    cat <<EOF
----------------------------------------------------------------------------------
Found an available mainline bootloader (Mainline u-boot), you can flash into EMMC.
----------------------------------------------------------------------------------
EOF
    while :; do
        # For [luci-app-amlogic] input parameter: SOC & DTB
        # When there is no input parameter, select manually
        if [[ "${AUTO_MAINLINE_UBOOT}" == "yes" ]]; then
            if [[ "${K510}" -eq "1" ]]; then
                yn="y"
            else
                yn="n"
            fi
        elif [[ "${AUTO_MAINLINE_UBOOT}" == "no" ]]; then
            yn="n"
        else
            read -p "Please choose whether to write the mainline bootloader to EMMC?  y/n " yn
        fi
        case $yn in
        y | Y)
            FLASH_MAINLINE_UBOOT=1
            break
            ;;
        n | N)
            FLASH_MAINLINE_UBOOT=0
            break
            ;;
        esac
    done
fi

if [[ "${FLASH_MAINLINE_UBOOT}" -eq "1" ]]; then
    echo -e "Write Mainline bootloader: [ ${MAINLINE_UBOOT} ]"
    dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=1 count=444 conv=fsync
    dd if=${MAINLINE_UBOOT} of=/dev/${EMMC_NAME} bs=512 skip=1 seek=1 conv=fsync
elif [[ -n "${ANDROID_UBOOT}" && -f "${ANDROID_UBOOT}" ]]; then
    echo -e "Write Android bootloader: [ ${ANDROID_UBOOT} ]"
    dd if=${ANDROID_UBOOT} of=/dev/${EMMC_NAME} bs=1 count=444 conv=fsync
    dd if=${ANDROID_UBOOT} of=/dev/${EMMC_NAME} bs=512 skip=1 seek=1 conv=fsync
else
    echo "Did not change the original bootloader."
fi

# fix wifi macaddr
if [ -x /usr/bin/fix_wifi_macaddr.sh ]; then
    /usr/bin/fix_wifi_macaddr.sh
fi

# mkfs
echo "Start creating file system ... "
echo "Create a boot file system ... "

echo "format boot partiton..."
mkfs.fat -n EMMC_BOOT -F 32 /dev/${EMMC_NAME}p1
mkdir -p /mnt/${EMMC_NAME}p1
sleep 2
umount -f /mnt/${EMMC_NAME}p1 2>/dev/null

echo "format rootfs1 partiton..."
ROOTFS1_UUID=$(/usr/bin/uuidgen)
mkfs.btrfs -f -U ${ROOTFS1_UUID} -L EMMC_ROOTFS1 -m single /dev/${EMMC_NAME}p2
mkdir -p /mnt/${EMMC_NAME}p2
sleep 2
umount -f /mnt/${EMMC_NAME}p2 2>/dev/null

echo "format rootfs2 partiton..."
ROOTFS2_UUID=$(/usr/bin/uuidgen)
mkfs.btrfs -f -U ${ROOTFS2_UUID} -L EMMC_ROOTFS2 -m single /dev/${EMMC_NAME}p3
mkdir -p /mnt/${EMMC_NAME}p3
sleep 2
umount -f /mnt/${EMMC_NAME}p3 2>/dev/null

# mount and copy
echo "Wait for the boot file system to mount ... "
i=1
max_try=10
while [ $i -le $max_try ]; do
    mount -t vfat /dev/${EMMC_NAME}p1 /mnt/${EMMC_NAME}p1 2>/dev/null
    sleep 2
    mnt=$(lsblk -l -o MOUNTPOINT | grep /mnt/${EMMC_NAME}p1)

    if [ "$mnt" == "" ]; then
        if [ $i -lt $max_try ]; then
            echo "Not mounted successfully, try again ..."
            i=$((i + 1))
        else
            echo "Cannot mount the boot file system, give up!"
            exit 1
        fi
    else
        echo "Successfully mounted."
        echo "copy boot ..."
        cd /mnt/${EMMC_NAME}p1
        rm -rf /boot/'System Volume Information/'
        (cd /boot && tar cf - .) | tar xf -
        sync

        echo "Edit uEnv.txt ..."
        cat >uEnv.txt <<EOF
LINUX=/zImage
INITRD=/uInitrd
FDT=/dtb/amlogic/${FDTFILE}
APPEND=root=UUID=${ROOTFS1_UUID} rootfstype=btrfs rootflags=compress=zstd console=ttyAML0,115200n8 console=tty0 no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1
EOF

        rm -f s905_autoscript* aml_autoscript*

        if [ ${K510} -eq 1 ]; then
            if [ -f ${UBOOT_OVERLOAD} ]; then
                cp -f -v ${UBOOT_OVERLOAD} u-boot.emmc
            elif [ -f "u-boot.ext" ]; then
                cp -f -v u-boot.ext u-boot.emmc
            fi
        fi

        mv -f boot-emmc.ini boot.ini
        mv -f boot-emmc.cmd boot.cmd
        mv -f boot-emmc.scr boot.scr

        sync
        echo "complete."
        cd /
        umount -f /mnt/${EMMC_NAME}p1
        break
    fi
done
echo "complete."

echo "Wait for the rootfs file system to mount ... "
i=1
while [ $i -le $max_try ]; do
    mount -t btrfs -o compress=zstd /dev/${EMMC_NAME}p2 /mnt/${EMMC_NAME}p2 2>/dev/null
    sleep 2
    mnt=$(lsblk -l -o MOUNTPOINT | grep /mnt/${EMMC_NAME}p2)
    if [ "$mnt" == "" ]; then
        if [ $i -lt $max_try ]; then
            echo "Not mounted successfully, try again ..."
            i=$((i + 1))
        else
            echo "Cannot mount rootfs file system, give up!"
            exit 1
        fi
    else
        echo "Successfully mounted"
        echo "Create folder ... "
        cd /mnt/${EMMC_NAME}p2
        btrfs subvolume create etc
        mkdir -p bin boot dev lib opt mnt overlay proc rom root run sbin sys tmp usr www .reserved .snapshots
        ln -sf lib/ lib64
        ln -sf tmp/ var
        sync
        echo "complete."

        COPY_SRC="root etc bin sbin lib opt usr www"
        echo "Copy data ... "
        for src in $COPY_SRC; do
            echo "copy [ $src ] ..."
            (cd / && tar cf - $src) | tar xf -
            sync
        done
        echo "Copy complete."
        sync

        echo "Update the relevant parameters of flippy-openwrt-release"
        sed -i '/FDTFILE/d' etc/flippy-openwrt-release 2>/dev/null
        echo "FDTFILE='${FDTFILE}'" >>etc/flippy-openwrt-release 2>/dev/null

        sed -i '/UBOOT_OVERLOAD/d' etc/flippy-openwrt-release 2>/dev/null
        echo "UBOOT_OVERLOAD='${UBOOT_OVERLOAD}'" >>etc/flippy-openwrt-release 2>/dev/null

        sed -i '/MAINLINE_UBOOT/d' etc/flippy-openwrt-release 2>/dev/null
        echo "MAINLINE_UBOOT='${MAINLINE_UBOOT}'" >>etc/flippy-openwrt-release 2>/dev/null

        sed -i '/ANDROID_UBOOT/d' etc/flippy-openwrt-release 2>/dev/null
        echo "ANDROID_UBOOT='${ANDROID_UBOOT}'" >>etc/flippy-openwrt-release 2>/dev/null

        sed -i '/SOC/d' etc/flippy-openwrt-release 2>/dev/null
        echo "SOC='${AMLOGIC_SOC}'" >>etc/flippy-openwrt-release 2>/dev/null
        echo "Update complete."
        sync

        cat >etc/docker/daemon.json <<EOF
{
  "bip": "172.31.0.1/24",
  "data-root": "/mnt/${EMMC_NAME}p4/docker/",
  "log-level": "warn",
  "log-driver": "json-file",
  "log-opts": {
     "max-size": "10m",
     "max-file": "5"
   },
  "registry-mirrors": [
     "https://docker.mirrors.ustc.edu.cn",
     "https://registry.cn-shanghai.aliyuncs.com",
     "https://hub-mirror.c.163.com"
   ]
}
EOF
        rm -rf opt/docker && ln -sf /mnt/${EMMC_NAME}p4/docker/ opt/docker >/dev/null
        rm -rf usr/bin/AdGuardHome && ln -sf /mnt/${EMMC_NAME}p4/AdGuardHome usr/bin/ >/dev/null

        echo "Edit configuration file ..."
        #cd /mnt/${EMMC_NAME}p2/usr/bin/
        #rm -f openwrt-install-amlogic openwrt-update-amlogic
        cd /mnt/${EMMC_NAME}p2/etc/rc.d
        ln -sf ../init.d/dockerd S99dockerd
        rm -f S??shortcut-fe
        if grep "sfe_flow '1'" ../config/turboacc >/dev/null; then
            if find ../../lib/modules -name 'shortcut-fe-cm.ko'; then
                ln -sf ../init.d/shortcut-fe S99shortcut-fe
            fi
        fi
        cd /mnt/${EMMC_NAME}p2/etc
        cat >fstab <<EOF
UUID=${ROOTFS1_UUID} / btrfs compress=zstd 0 1
LABEL=EMMC_BOOT /boot vfat defaults 0 2
#tmpfs /tmp tmpfs defaults,nosuid 0 0
EOF

        cd /mnt/${EMMC_NAME}p2/etc/config
        cat >fstab <<EOF
config  global
        option anon_swap '0'
        option anon_mount '1'
        option auto_swap '0'
        option auto_mount '1'
        option delay_root '5'
        option check_fs '0'

config  mount
        option target '/overlay'
        option uuid '${ROOTFS1_UUID}'
        option enabled '1'
        option enabled_fsck '1'
        option fstype 'btrfs'
        option options 'compress=zstd'

config  mount
        option target '/boot'
        option label 'EMMC_BOOT'
        option enabled '1'
        option enabled_fsck '1'
        option fstype 'vfat'

EOF

        echo -n "Create initial etc snapshot -> .snapshots/etc-000"
        cd /mnt/${EMMC_NAME}p2 &&
            btrfs subvolume snapshot -r etc .snapshots/etc-000
        sync

        cd /
        umount -f /mnt/${EMMC_NAME}p2
        break
    fi
done
echo "complete."

echo "Create a shared file system."
mkdir -p /mnt/${EMMC_NAME}p4

# When there is no input parameter, select manually
if [[ -n "${SHARED_FSTYPE}" ]]; then
    TARGET_SHARED_FSTYPE=${SHARED_FSTYPE}
else
    cat <<EOF
---------------------------------------------------------------------------------
Please select the type of shared file system:
1. ext4:  [Default options] suitable for general use.
2. btrfs: Which can extend the service life of ssd/mmc.
3. f2fs:  Fast reading and writing speed, but the compatibility is slightly poor.
4. xfs:   Very good file system, alternative to ext4.
---------------------------------------------------------------------------------
EOF
    read TARGET_SHARED_FSTYPE
fi
case $TARGET_SHARED_FSTYPE in
2 | btrfs)
    mkfs.btrfs -f -L EMMC_SHARED -m single /dev/${EMMC_NAME}p4 >/dev/null
    mount -t btrfs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
    ;;
3 | f2fs)
    mkfs.f2fs -f -l EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
    mount -t f2fs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
    ;;
4 | xfs)
    mkfs.xfs -f -L EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
    mount -t xfs /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
    ;;
*)
    mkfs.ext4 -F -L EMMC_SHARED /dev/${EMMC_NAME}p4 >/dev/null
    mount -t ext4 /dev/${EMMC_NAME}p4 /mnt/${EMMC_NAME}p4
    ;;
esac
mkdir -p /mnt/${EMMC_NAME}p4/docker /mnt/${EMMC_NAME}p4/AdGuardHome/data
sync

echo "Successful installed, please unplug the USB, re-insert the power supply to start the openwrt."
exit 0
